package iontec.behavior;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.os.Bundle;

public class Behaviors extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1; 
	private GoBDbAdapter mDbHelper;
	private Cursor mBehaviorCursor;
	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1; 
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fillData();
        registerForContextMenu(getListView()); 
    }
   
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_del);
    } 
 
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_add);
        return true;
    }
 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Cursor c = mBehaviorCursor;
        c.moveToPosition(position);
        Intent i = new Intent(this, BehaviorEdit.class);
        i.putExtra(GoBDbAdapter.KEY_ROWID, id);
        i.putExtra(GoBDbAdapter.KEY_NAME, c.getString(
                c.getColumnIndexOrThrow(GoBDbAdapter.KEY_NAME)));
        i.putExtra(GoBDbAdapter.KEY_VAL, c.getInt(
                c.getColumnIndexOrThrow(GoBDbAdapter.KEY_VAL)));
        startActivityForResult(i, ACTIVITY_EDIT);
    } 

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteBehavior(info.id);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }
 
 	@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createBehavior();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
	private void fillData() {
		mDbHelper = new GoBDbAdapter(this);
		mDbHelper.open();

		mBehaviorCursor = mDbHelper.fetchAllBehaviors(0);
		startManagingCursor(mBehaviorCursor);

		String[] from = new String[] {"name", "value"};
		int[] to = new int[] { R.id.b_name, R.id.b_value};
		
		SimpleCursorAdapter behaviors = 
			new SimpleCursorAdapter(this, R.layout.behavior_item, mBehaviorCursor, from, to);

		ListView lv = getListView();
		lv.setAdapter(behaviors);
	}	
	
	private void createBehavior() {
        Intent i = new Intent(this, BehaviorEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode==RESULT_OK){ 
			Bundle extras = intent.getExtras();
			switch(requestCode) {
			case ACTIVITY_CREATE:
				String name = extras.getString(GoBDbAdapter.KEY_NAME);
				Integer value = extras.getInt(GoBDbAdapter.KEY_VAL);
				mDbHelper.createBehavior(name,value);
				fillData();
				break;
			case ACTIVITY_EDIT:
				
			Long rowId = extras.getLong(GoBDbAdapter.KEY_ROWID);
			if (rowId != null) {
				String editname = extras.getString(GoBDbAdapter.KEY_NAME);
				Integer editval = extras.getInt(GoBDbAdapter.KEY_VAL);
				mDbHelper.updateBehavior(rowId, editname, editval);
			}
				
				fillData();
				break;
			} 
		}
	}
}



